package top.chmin.project.cloud.sys.dict.data;

import cn.hutool.core.bean.BeanUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.chmin.project.cloud.common.Page;
import top.chmin.project.cloud.tool.CheckTool;

import java.util.List;


/**
 * chmin
 * 2024-01-26 14:02:41
 * cloud-code
 */
@Service
@Transactional
public class DictDataService {

    @Resource
    DictDataPlugin plugin;

    public int save(DictDataSaveRest.P p) {
        plugin.checkSave(p);
        DictData dictData = BeanUtil.toBean(p, DictData.class);
        plugin.beforeSave(dictData);
        int result = dictData.forInsert().execute();
        plugin.afterSave(p, dictData, result);
        return result;
    }

    public DictDataSelectRest.R select(DictDataSelectRest.P p) {
        plugin.checkSelect(p);
        DictDataCondition c = DictDataCondition.forSelect().id().eq(p.getId());
        plugin.beforeSelect(p, c);
        DictDataSelectRest.R r = c.selectFirst(DictDataSelectRest.R.class);
        plugin.afterSelect(p, r);
        return r;
    }

    public int update(DictDataUpdateRest.P p) {
        plugin.checkUpdate(p);
        DictData dictData = BeanUtil.toBean(p, DictData.class);
        plugin.beforeUpdate(dictData);
        int result = dictData.forUpdate().id().eq().execute();
        plugin.afterUpdate(p, dictData, result);
        return result;
    }

    public int delete(DictDataDeleteRest.P p) {
        plugin.checkDelete(p);
        DictData dictData = BeanUtil.toBean(p, DictData.class);
        plugin.beforeDelete(dictData);
        int result = dictData.forDelete().id().eq().execute();
        plugin.afterDelete(p, dictData, result);
        return result;
    }

    public Page<DictDataPageRest.R> page(DictDataPageRest.P p) {
        plugin.checkPage(p);
        DictDataCondition c = DictDataCondition.forSelect();
        c = p.isDesc() ? c.id().desc() : c.id().asc();
        plugin.beforePage(p, c);
        Page<DictDataPageRest.R> r = c.selectPage(DictDataPageRest.R.class);
        /*###########auto#start###########*/
        /*###########auto##end############*/
        /* append $serviceRelateAppender$ */
        /*###########auto#start###########*/
        /*###########auto##end############*/
        /* append $serviceDictAppender$ */
        plugin.afterPage(p, r);
        return r;
    }

    public List<DictDataSelectListByType.R> selectListByType(DictDataSelectListByType.P p) {
        CheckTool.notNull(p.getTypeCode());
        return DictDataCondition.forSelect()
                .typeCode().eq(p.getTypeCode())
                .selectList(DictDataSelectListByType.R.class);
    }
    /* append $serviceSelectOptionMethodAppender$ */
}
